<?php
/* 
 * Copyright (c) 2011 Antoine d'Otreppe de Bouvette
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

/**
 * Represents a HTTP request, probably issued by the visitor's browser. 
 */
class Request {
    /**
     * The request URI
     * 
     * @var string
     */
    private $uri;
    
    /**
     * The request method (uppercased)
     * 
     * @var string
     */
    private $method;
    
    /**
     * The arguments parsed from the URI
     * 
     * @var string[]
     */
    private $arguments;
    
    /**
     * The name of the corresponding route, if any
     * 
     * @var string
     */
    private $route;
    
    /**
     * The parsed request body
     * 
     * @var string[]
     */
    private $body;
    
    /**
     * @var boolean
     */
    private $sessionStarted = false;
    
    /**
     * @param string $method 
     */
    public function setMethod($method) {
        $this->method = strtoupper($method);
    }
    
    /**
     * Returns the argument as extracted from the URI.
     * 
     * @param string $name
     * @param string $default
     * @return string the argument value, or $default
     */
    public function getArgument($name, $default=null) {
    	return array_get($this->arguments, $name, $default);
    }
    
    /**
     * Returns a value from the request body.
     * 
     * @param string $name
     * @param string $default
     * @return string the body value, or $default
     */
    public function getBodyValue($name, $default=null) {
        return array_get($this->body, $name, $default);
    }
    
    /**
     * Returns a session container for this class of object.
     * 
     * @param object $object
     * @return SessionContainer 
     */
    public function getSessionContainer($object) {
        if (!$this->sessionStarted) {
            session_start();
            $this->sessionStarted = true;
        }
        
        return new SessionContainer(get_class($object));
    }
    
    public function getUri() {
        return $this->uri;
    }

    public function setUri($uri) {
        $this->uri = $uri;
    }

    public function getArguments() {
        return $this->arguments;
    }

    public function setArguments($arguments) {
        $this->arguments = $arguments;
    }

    public function getRoute() {
        return $this->route;
    }

    public function setRoute($route) {
        $this->route = $route;
    }

    public function getBody() {
        return $this->body;
    }

    public function setBody($body) {
        $this->body = $body;
    }
    
    /**
     * @return string the HTTP request method, always uppercase
     */
    public function getMethod() {
        return $this->method;
    }
}
